<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define('TEST_DIR', dirname(__FILE__));
define('TEST_DIR_LIB', dirname(TEST_DIR) . '/src');

require_once 'yay_mock.php';
require_once 'yay_convenience.php';
require_once TEST_DIR_LIB . DIRECTORY_SEPARATOR . 'php53compat' . DIRECTORY_SEPARATOR . 'common.php';

function test_getClassList(
	array &$classList, RecursiveDirectoryIterator $directory)
{
	foreach ($directory as $fileInfo)
	{
		if ($fileInfo->isDir())
		{
			test_getClassList($classList, $directory->getChildren());
		}
		elseif (substr($fileInfo->getFilename(), -4) === '.php')
		{
			$classList[substr($fileInfo->getFilename(), 0, -4)] =
				$fileInfo->getPathname();
		}
	}
}

function test_autoload($className)
{
	static $classList = array();
	
	if (empty($classList))
	{
		test_getClassList($classList, new RecursiveDirectoryIterator(TEST_DIR));
		test_getClassList($classList, new RecursiveDirectoryIterator(TEST_DIR_LIB));
	}
	
	if (isset($classList[$className]))
	{
		require_once $classList[$className];
	}
}

spl_autoload_register('test_autoload');